package com.zp.self.module.工具.json转换.json_csv;

import org.apache.commons.io.FileUtils;
import org.json.*;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author ：by peng.zeng
 * @date ：Created 2021/9/3 15:05
 * @description ：整理
 */
public class Json2Csv多文件合并去重 {
    public static void main(String[] args) throws IOException {
        m4();
    }

    private static void m4() throws IOException {
        String outPathPart2 = "C:\\Users\\peng.zeng\\Desktop\\data-6.csv";
        File file = new File(outPathPart2);

        //step 0.当前版本
        Map<String, String> id_vesrsion = getIdVesrsionMap();

        Map<String, List<JSONObject>> id_list = new LinkedHashMap<>();
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data4-eu.json");
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data6-eu.json");
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data8-eu.json");
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data10-eu.json");

        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data4-usa.json");
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data6-usa.json");
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data8-usa.json");
        getObjects(id_list, "C:\\Users\\peng.zeng\\Desktop\\data10-usa.json");



        //step 2.对时间做排序
        JSONArray resultJsonArraya = new JSONArray();
        for (String s : id_list.keySet()) {
            id_list.get(s).sort(Comparator.comparing(o -> o.getString("dy")));
            List<JSONObject> list = id_list.get(s);
            int size = list.size();
            JSONObject intiObj = list.get(0);

            String intiv2 = intiObj.getString("v2");//初始版本
            String proiv2 = intiObj.getString("v2");//上一个版本

            for (int i = 0; i < size; i++) {
                JSONObject jsonObject = list.get(i);
                if(i==0){
                    jsonObject.put("current software version",jsonObject.getString("v2"));
                    jsonObject.put("original software version",jsonObject.getString("v2"));
                    jsonObject.put("update from software version to next software version","");
                }else{
                    jsonObject.put("current software version",id_vesrsion.get(jsonObject.getString("deviceId")));
                    jsonObject.put("original software version",intiv2);
                    jsonObject.put("update from software version to next software version",proiv2+" --> " +jsonObject.getString("v2"));
                    proiv2 =jsonObject.getString("v2");
                }
                jsonObject.remove("v2");
                resultJsonArraya.put(jsonObject);
            }
        }

        //step 3.导出数据
        FileUtils.writeStringToFile(file, CDL.toString(resultJsonArraya), Charset.defaultCharset(), true);

    }

    private static Map<String, String> getIdVesrsionMap() throws IOException {
        Map<String, String> id_vesrsion = new HashMap<>();
        JSONArray cur_versionJsonArray = getJson("C:\\Users\\peng.zeng\\Desktop\\cur_version.json");
        for (int i = 0; i < cur_versionJsonArray.length(); i++) {
            try {
                JSONObject jsonObject = cur_versionJsonArray.getJSONObject(i);
                if(jsonObject.get("Software_version_and_release2") ==null ||
                        jsonObject.get("Software_version_and_release2").toString() .equals("null")){
                    continue;
                }
                id_vesrsion.put(jsonObject.getString("deviceId"),jsonObject.getString("Software_version_and_release2"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return id_vesrsion;
    }

    private static void getObjects(Map<String, List<JSONObject>> id_list, String s2) throws IOException {
        JSONArray jsonArray4 = getJson(s2);
        mergeJsonArray(id_list, jsonArray4);
    }

    /**
     * Description: 将jsonArraya的内容整合
     * @param id_list 存储内容
     * @param jsonArraya 数据
     */
    private static void mergeJsonArray(Map<String, List<JSONObject>> id_list, JSONArray jsonArraya) {
        int length = jsonArraya.length();
        for (int i = 0; i < length; i++) {
            JSONObject jsonObject = jsonArraya.getJSONObject(i);
            String deviceId = jsonObject.getString("deviceId");
            if(!id_list.containsKey(deviceId)){
                ArrayList<JSONObject> list = new ArrayList<>();
                list.add(jsonObject);
                id_list.put(deviceId,list);
            }else{
                //删除重复数据,判断与上一条数据是否相同
                List<JSONObject> list = id_list.get(deviceId);
                JSONObject json = list.get(list.size() - 1);
                if(json.getString("v2").equals(jsonObject.getString("v2"))  ){
                   continue;
                }
                list.add(jsonObject);
            }

        }
    }


    private static JSONArray getJson(String inPath2) throws IOException {
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream(inPath2));
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8), 5 * 1024 * 1024);// 用5M的缓冲读取文本文件
        JSONTokener jsonTokener = new JSONTokener(reader);
        JSONArray objects = new JSONArray(jsonTokener);
        fis.close();
        reader.close();
        return objects;
    }
}
